// index.ts

const { picList, ruleList, getCode } = require("../../utils/api")

// 获取应用实例
const app = getApp()

Page({
  data: {
    picList: [],
    showRule: false,//规则弹窗 
    ruleMsg: {},
    page: 1,
    limit: 10,
    count: null,
    onBottom: false,
    isJwt: false,
    showLogin: false,
    page: 1,
    limit: 10,
    count: null,
    onBottom: false,
    fatherData: {},
    showModal: false,
    showErrModal: false,
    errData: '',
    myData: ''
  },

  onLoad() {
    this.setData({
      page: 1
    })


    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }

    this.getPageData()
  },

  onShow() {
    const pages = getCurrentPages()
    const perpage = pages[pages.length - 1]
    perpage.onLoad()

    this.data.myData = perpage.data.preData

    if (this.data.myData) {
      let resStr = this.data.myData
      let strIndex = resStr.indexOf('goodsurl=') + 9
      let resParams = resStr.substr(strIndex)
      this.getCodeMsg(resParams)

    }
    this.getPic()
  },

  //扫码方法
  getScanCode() {
    wx.navigateTo({
      url: '/component/camerapage/index',
    });

  },
  //获取二维码数据
  async getCodeMsg(url) {
    let that = this
    let params = {
      goodsurl: url
    }
    await getCode(params).then((res) => {
      if (res.errcode == 0) {
        that.setData({
          showModal: true,
          fatherData: res.data,
          "fatherData.numRes": res.data.canscore * Number(res.data.multiplier),
          "fatherData.nexttime": (res.data.nexttime).substring(0, 10),
          "fatherData.endtime": (res.data.endtime).substring(0, 10),
          "fatherData.goodsurl": url,

        })

        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        currPage.setData({
          preData: ''
        })



      } else {
        that.setData({
          showErrModal: true,
          errData: res.errmsg,
        })
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        currPage.setData({
          preData: ''
        })


      }
    })
  },

  getPageData() {
    let that = this
    const jwt = wx.getStorageSync('jwt');
    if (jwt) {
      that.setData({
        isJwt: true
      })
    } else {
      that.setData({
        isJwt: false
      })
    }

  },

  //获取图鉴
  async getPic() {

    let params = {
      page: this.data.page,
      limit: this.data.limit
    }
    await picList(params).then((res) => {
      if (res.errcode == 0) {
        if (this.data.onBottom == false) {
          this.setData({
            count: res.data.count,
            picList: res.data.series
          })
        } else {
          let newArr = [...this.data.picList, ...res.data.series]
          let uniqueArr = [...new Set(newArr)]
          this.setData({
            count: res.data.count,
            picList: uniqueArr
          })
        }

      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',

        });
      }

    })

  },

  async getRules() {
    let params = {
      type: '6'
    }
    await ruleList(params).then((res) => {
      if (res.errcode == 0) {
        this.setData({
          showRule: true,
          ruleMsg: res.data
        })
      } else {
      }
    })
  },

  onPicDetail(e) {
    let that = this
    if (!that.data.isJwt) {
      that.setData({
        showLogin: true
      })
    } else {
      wx.navigateTo({ url: `/packageA/pages/picdetail/index?skuid=${e.currentTarget.dataset.item.series_num}` })
    }

  },

  onHelp() {
    this.getRules()
  },

  onReachBottom() {
    if (this.data.page * this.data.limit >= this.data.count) {
      this.setData({
        onBottom: false
      })
      wx.showToast({
        title: '没有更多了',
        icon: 'none',

      });

    } else {
      this.setData({
        onBottom: true
      })
      this.data.page++
      this.getPic()
    }

  }


})
